package keeto.com.vn.bms.ui.fragment;


import keeto.com.vn.bms.config.BMSHomeCagetory;
import keeto.com.vn.bms.config.IBMSConfig;
import keeto.com.vn.bms.ui.activity.BMSAdminActivity;
import keeto.com.vn.bms.ui.activity.BMSChatActivity;
import keeto.com.vn.bms.ui.activity.BMSEducationActivity;
import keeto.com.vn.bms.ui.activity.CreateScheduleActivity;
import keeto.com.vn.bms.ui.activity.ListProductActivity;
import keeto.com.vn.bms.ui.activity.ListPromotionActivity;
import keeto.com.vn.bms.ui.activity.ListSeminarActivity;
import keeto.com.vn.bms.ui.activity.ListSurgeryActivity;
import keeto.com.vn.bms.ui.activity.R;
import keeto.com.vn.bms.ui.adapter.CategoryBMSHomeFragmentAdapter;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class CategoryBMSHomeFragment extends BaseFragment implements
OnItemClickListener {
	String TAG = CategoryBMSHomeFragment.class.getName();

	private GridView mGridView;
	private CategoryBMSHomeFragmentAdapter mAdapter;
	private int mType = 0;
	private int gridHeight;
private BMSSharedPreferences preferences;

	public static CategoryBMSHomeFragment newInstance(){
		CategoryBMSHomeFragment fragment = new CategoryBMSHomeFragment();
		Bundle bundle = new Bundle();
		fragment.setArguments(bundle);
		return fragment;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		preferences = new BMSSharedPreferences(getActivity());
		mType = preferences.getUserPermission(getActivity());
	}


	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.category_home_fragment_layout, container,false);
		initView(view, mType );
		return view;
	}

	private void initView(View v, final int type){
		mGridView = (GridView)v.findViewById(R.id.grid_home);
		ViewTreeObserver treeObserver = mGridView.getViewTreeObserver();
		treeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

			@SuppressLint("NewApi") @SuppressWarnings("deprecation")
			@Override
			public void onGlobalLayout() {
				// TODO Auto-generated method stub
				gridHeight = mGridView.getHeight()-15;
				Log.e(TAG,"Size" + gridHeight );
				mAdapter = new CategoryBMSHomeFragmentAdapter(getActivity(), type,gridHeight);
				mGridView.setAdapter(mAdapter);
				mAdapter.notifyDataSetChanged();
				if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN){
					mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
				} else{
					mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				}
			}
		});
		mGridView.setOnItemClickListener(this);
		
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		int key =	mAdapter.getItem(position).getCategoryKey();
		switch (key) {
		case BMSHomeCagetory.ICON_SURGERY:
			if (mType == IBMSConfig.KEY_TYPE_DOCTOR){  //neu la thang bac si thi cho phep tao lịch mo
				CreateScheduleActivity.actionLaunch(getActivity());

			}else{ //neu la thang admin thi cho hien thi cac danh sach lich mo
				ListSurgeryActivity.actionLaunch(getActivity());

			}
			break;
		case BMSHomeCagetory.ICON_ADMIN:
			BMSAdminActivity.actionLaunch(getActivity());
			break;
		case BMSHomeCagetory.ICON_SOPPORT:
			BMSChatActivity.actionLaunch(getActivity());
			break;
		case BMSHomeCagetory.ICON_INFO:
			ListProductActivity.actionLaunch(getActivity());
			break;
		case BMSHomeCagetory.ICON_PROMOTION:
			ListPromotionActivity.actionLaunch(getActivity());
			break;
		case BMSHomeCagetory.ICON_EDUCATION:
			BMSEducationActivity.actionLaunch(getActivity());
			break;
		case BMSHomeCagetory.ICON_SEMINAR:
			ListSeminarActivity.actionLaunch(getActivity());
			break;


		default:
			break;
		}

	}



}
